package cn.hengzhu.main_manager.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.dao.ManageUserMapper;
import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.repository.param.MyInfoParam;
import cn.hengzhu.main_manager.repository.param.MyPsssWordParam;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.Md5Util;
import cn.hengzhu.main_manager.utils.UserContext;
import cn.hengzhu.main_manager.utils.ann.SystemControllerLog;

@Controller
@RequestMapping("/main")
public class MyCenterController {
	
	@Autowired
	private ManageUserMapper manageUserMapper;

	/**
	 * 修改我的信息
	 */
	@SystemControllerLog(description="修改个人信息")
	@ResponseBody
	@RequestMapping(value="/updateMyInfo",method = RequestMethod.POST)
	public AjaxResult updateMyInfo(MyInfoParam param) {
		ManageUser user = UserContext.getUser();
		ManageUser manageUser = new ManageUser();
		manageUser.setEmail(param.getEmail());
		manageUser.setMobilePhone(param.getMobilePhone());
		manageUser.setPassWord(param.getPassWord());
		manageUser.setRealName(param.getRealName());
		manageUser.setManagerId(user.getManagerId());
		try {
			manageUserMapper.updateByPrimaryKeySelective(manageUser);
			return new AjaxResult();
		} catch (Exception e) {
			return new AjaxResult("修改失败，请重试");
		}

	}

	/**
	 * 修改我的密碼
	 */
	@SystemControllerLog(description="修改个人密码")
	@ResponseBody
	@RequestMapping(value="/updateMyPsssWord",method = RequestMethod.POST)
	public AjaxResult updateMyPsssWord(MyPsssWordParam param) {
		ManageUser user = UserContext.getUser();
		ManageUser manageUser = new ManageUser();
		String oldPssWord = param.getOldPassWord();
		String newPassWord = param.getNewPassWord();
		String surePassWord = param.getSurePassWord();
		if (StringUtils.isNotBlank(oldPssWord) && user.getPassWord().equals(Md5Util.hash(oldPssWord))) {
			if (newPassWord.equals(surePassWord)) {
				manageUser.setPassWord(Md5Util.hash(surePassWord));
				manageUser.setManagerId(user.getManagerId());
				try {
					manageUserMapper.updateByPrimaryKeySelective(manageUser);
					return new AjaxResult();
				} catch (Exception e) {
					return new AjaxResult("修改失败，请重试");
				}
			} else {
				return new AjaxResult("两次密码输入不一致");
			}
		} else {
			return new AjaxResult("原密码错误");
		}
	}


}
